#!/bin/bash

if [[ $(id -u) != 0 ]]; then
    echo "Please run as root or use sudo"
    exit 1
fi

if [[ $# -lt 2 ]]; then
    echo "Usage: $(basename $0) share_point share_path"
    exit 1
fi

SMB_CONF=/etc/samba/smb.conf

share_point=$1
share_path=$2

USERNAME=${SUDO_USER}
if [ -z ${USERNAME} ]; then
    USERNAME=root
fi

echo "USERNAME: ${USERNAME}"

set -e

if [[ ! -f ${SMB_CONF} ]]; then
    apt-get install -y samba
fi

if [[ "" == "$(grep -iw ${share_point} ${SMB_CONF})" ]]; then
cat << EOF >> ${SMB_CONF}
[${share_point}]
   path = ${share_path}
   browseable = yes
   guest ok = yes
   write list = ${USERNAME}
   create mask = 0644
   directory mask = 0755
EOF
else
    echo "[${share_point}] already in ${SMB_CONF}"
fi

echo "Now you need do: smbpasswd -a ${USERNAME}"

